package org.eduapp.trains.web.action;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForward;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.util.MessageResources;
import org.eduapp.trains.Constants;
import org.hibernate.HibernateException;

public class BaseAction extends DispatchAction{
	
	public static class AjaxResponse{
		public AjaxResponse(){}
		public static final String RESPONSE_ERROR = "fail";
		public static final String RESPONSE_SUCCESS = "success";
	}
	
	/**
	 * 
	 * @param request
	 * @return
	 * @author <a href="mailto:z405656232x@163.com">JeccyZhao</a>
	 */
	protected boolean isAjaxReq(HttpServletRequest request){
		return request.getParameter(Constants.DefaultAjaxParamFlag) != null;
	}
	
	/**
	 * 
	 * @param response
	 * @param msg
	 * @throws IOException
	 * @author <a href="mailto:z405656232x@163.com">JeccyZhao</a>
	 */
	protected ActionForward ajaxPrint(HttpServletResponse response,String msg) {
		try{
			response.getWriter().write(msg);
		}catch(IOException e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 
	 * @param request
	 * @param key
	 * @return
	 * @author <a href="mailto:z405656232x@163.com">JeccyZhao</a>
	 */
	protected String getMessageFromResource(HttpServletRequest request,String key){
		return getMessageFromResource(request,key,null,null);
	}
	
	/**
	 * 
	 * @param request
	 * @param key
	 * @param args
	 * @return
	 * @author <a href="mailto:z405656232x@163.com">JeccyZhao</a>
	 */
	protected String getMessageFromResource(HttpServletRequest request,String key,Object[] args){
		return getMessageFromResource(request,key,args,null);
	}
	
	/**
	 * 
	 * @param rs
	 * @param key
	 * @return
	 * @author <a href="mailto:z405656232x@163.com">JeccyZhao</a>
	 */
	protected String getMessageFromResource(HttpServletRequest request,String key,Object[] args,String cfgKey){
		MessageResources rs = cfgKey != null ? getResources(request, cfgKey) : getResources(request);
		return rs != null ? (args == null ? rs.getMessage(key) : rs.getMessage(key, args)): "";
	}
	
	/**
	 * 
	 * @param request
	 * @return
	 * @author Zhao.Xiang
	 */
	protected String getRefererUrl(HttpServletRequest request){
		return request.getHeader("REFERER") != null ? request.getHeader("REFERER") : null;
	}
	
	/**
	 * 
	 * @param obj
	 * @return
	 */
	protected void eagerInitial(Object model){
		if (model != null){
			try{
				//Hibernate.initialize(model);
			} catch(HibernateException e){
				log.error("Proxy initialize error: " + e.getMessage());
			}
		}
	}
	
	/**
	 * 
	 * @param metaPath
	 * @author <a href="mailto:z405656232x@163.com">JeccyZhao</a>
	 */
	public static void removePhyImageFile(String metaPath){
		if(metaPath != null && !"".equals(metaPath)){
			File metaFile = new File(
					Constants.WebHomePath + 
					Constants.DefaultFileUploadDir + 
					Constants.FileSeparator + 
					metaPath);
			if(metaFile.exists()){
				metaFile.delete();
			}
			
			File metaSmallFile = new File(
					Constants.WebHomePath + 
					Constants.DefaultFileUploadDir + 
					Constants.FileSeparator + "small" +
					Constants.FileSeparator + metaPath);
			
			if(metaSmallFile.exists()){
				metaSmallFile.delete();
			}
			
			File metaMiddleFile = new File(
					Constants.WebHomePath + 
					Constants.DefaultFileUploadDir + 
					Constants.FileSeparator + "middle" +
					Constants.FileSeparator + metaPath);
			if(metaMiddleFile.exists()){
				metaMiddleFile.delete();
			}
			
			File metaBigFile = new File(
					Constants.WebHomePath + 
					Constants.DefaultFileUploadDir + 
					Constants.FileSeparator + "big" +
					Constants.FileSeparator + metaPath);
			if(metaBigFile.exists()){
				metaBigFile.delete();
			}
		}
	}
	
	/**
	 * 
	 * @param request
	 * @return
	 */
	public static String getHostUrl (HttpServletRequest request) {
		if (request != null) {
			String path = request.getContextPath();  
			String basePath = request.getScheme()+"://"+request.getServerName()+
				(request.getServerPort() == 80 ? "" : (":"+request.getServerPort())) +path+"/";
			return basePath;
		}
		return null;
	}
	
}